//
// Created by denglibin on 2021/2/3.
//
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#define true 1
#define false 0
typedef int bool;
static const int WIN_WIDTH = 1366;
static const int WIN_HEIGHT = 768;
static SDL_Window *win; //窗口
static SDL_Surface *surface; // 窗口显示层
static SDL_Surface *bgSurface; // 背景图片表示层
static bool quit = false; //游戏状态
static SDL_Renderer *winRenderer;//主窗口渲染器
static const char * BG_IMG = "img/bg_1366_768.bmp";
static const char * ARROW_YELLOW_IMG = "img/arrow_yellow.png";
static const char * ARROW_WHITE_IMG = "img/arrow_white.png";
/**
 * 初始化
 * @return
 */
static bool init(){
    //对SDL库进行初始化
    int ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); //初始化音频，视频子系统
    if(ret < 0){
        printf("SDL_Init ERROR:%s\n", SDL_GetError());
        return false;
    }
    // 创建窗口
     win =  SDL_CreateWindow("魂斗罗2020", //标题
                     SDL_WINDOWPOS_UNDEFINED, //位置x坐标，使用操作系统默认
                     SDL_WINDOWPOS_UNDEFINED, //位置y坐标
                     WIN_WIDTH,
                     WIN_HEIGHT,
                     SDL_WINDOW_SHOWN); //显示     SDL_WINDOW_FULLSCREEN
    if(win == NULL){
        printf("SDL_CreateWindow ERROR:%s\n", SDL_GetError());
        return false;
    }

    surface = SDL_GetWindowSurface(win);//获取窗口显示层

    //主窗口渲染器
    winRenderer = SDL_CreateRenderer(
            win,
            -1,  //序号 -1表示第一个
            SDL_RENDERER_ACCELERATED//使用硬件加速
             | SDL_RENDERER_PRESENTVSYNC); //和刷新频率同步

    return true;
}
/**
 * 加载资源
 * @return
 */
static bool loadSource(){
    bgSurface = SDL_LoadBMP(BG_IMG);
    if(bgSurface == NULL){
        printf("SDL_LoadBMP ERROR:%s\n", SDL_GetError());
        return false;
    }
    return true;
}
/**
 * 加载图片 返回纹理
 * @param file
 * @return 纹理
 */
static SDL_Texture* loadImage(const char * file){
    SDL_Texture * texture = IMG_LoadTexture(winRenderer, file);
    if(texture == NULL){
        printf("IMG_LoadTexture ERROR:%s\n", SDL_GetError());
        exit(1);
    }
    return texture;
}

/**
 * 渲染图片
 */
static void showImage(const char *file, int  x, int y){
    SDL_Texture* texture =  loadImage(file); //纹理
    SDL_Rect  pos; //位置
    pos.x = x;
    pos.y = y;
    //获取纹理宽高
    SDL_QueryTexture(texture, NULL, NULL, &pos.w, &pos.h);
    //将纹理复制到渲染器上
    SDL_RenderCopy(winRenderer, //渲染器
                   texture,
                   NULL,
                   &pos);
}

/**
 * 键盘按下事件
 * @param e
 */
static void keyDownEvent(SDL_Event *e){
    switch (e->key.keysym.sym) {
        case SDLK_ESCAPE:
            quit = true;
            break;
        case SDLK_DOWN:
            showImage(BG_IMG, 0, 0);
            showImage(ARROW_WHITE_IMG, 100, 100);
            showImage(ARROW_YELLOW_IMG, 100, 300);
            break;
        case SDLK_UP:
            showImage(BG_IMG, 0, 0);
            showImage(ARROW_YELLOW_IMG, 100, 100);
            showImage(ARROW_WHITE_IMG, 100, 300);
            break;
        default:
            break;
    }
    SDL_RenderPresent(winRenderer); //展示
}

static void close(){
    // 清理资源
    //SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);
    SDL_DestroyRenderer(winRenderer);
    SDL_DestroyWindow(win);
    SDL_Quit();
}
 int main1(int argc, char* argv[]){
    //游戏初始化
    int ret = init();
    if(ret == false){
        return 1;
    }
    // 加载图像等资源
    ret = loadSource();
    if(ret == false){
        return 1;
    }
    SDL_Rect fillPos;
    fillPos.x = 0;
    fillPos.y = 0;
    fillPos.w = 300;
    fillPos.h = 300;
    //加载背景图片到主窗口
    SDL_BlitSurface(bgSurface,  //填充源
                    &fillPos, //填充源选择的区域
                    surface, // 目标
                    &fillPos); //填充的左上角的起始位置

    //SDL使用了2级缓冲技术，不会立即显示效果,刷新一下
    SDL_UpdateWindowSurface(win);
    SDL_Event  event;
    while (!quit)
    {//主消息循环
        SDL_WaitEvent(&event);

        switch (event.type)
        {
            //用户从菜单要求退出程序
            case SDL_QUIT:
                quit = 1;
                break;
            case SDL_KEYDOWN:
                keyDownEvent(&event);
                break;
            default:
                break;
        }
    }

    close();
    return 0;
}